﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Xml.Linq;

namespace ConnectionAnalyzer
{
    public partial class DebugScreen : Form
    {
        public DebugScreen()
        {
            InitializeComponent();
            WebDownloader.tbOutPut = tbOutput;
        }

        public static string GetCurrent_Directory()
        {
            return Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        }
        
        private void btGo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbOutput.Text))
            {
                string debugFileName = GetCurrent_Directory() + "\\OutPut.xml";
                XDocument db = null;
                if (File.Exists(debugFileName))
                {
                    db = XDocument.Load(debugFileName);
                }
                else
                    db = new XDocument(
                        new XDeclaration("1.0", "utf-8", "yes"),
                        new XElement("debug"));

                db.Root.Add(new XElement("OutPut",
                    new XAttribute("date", DateTime.Now.ToString()), tbOutput.Text));

                db.Save(debugFileName);

                MessageBox.Show("Output Saved to: " + debugFileName);
                return;
            }

            btGo.Enabled = false;
            Application.DoEvents();

            tbOutput.Text = "";
            Authentication auth = new Authentication(tbUser.Text, tbPassword.Text, tbOutput);
            auth.Authenticate();

            btGo.Enabled = true;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}